home *** CD-ROM | disk | FTP | other *** search
- Path: prairie.nodak.edu!not-for-mail
- From: wstark@prairie.nodak.edu (Just Me)
- Newsgroups: comp.lang.c
- Subject: Re: Schildt <- Advanced Books
- Date: 11 Feb 1996 06:16:50 -0600
- Organization: North Dakota Higher Education Computing Network (NDHECN)
- Message-ID: <4fkmni$589@prairie.nodak.edu>
- References: <8BA8405.02C70020DE.uuout@sourcebbs.com>
- NNTP-Posting-Host: prairie.nodak.edu
- Mime-Version: 1.0
- Content-Type: text/plain; charset=US-ASCII
- Content-Transfer-Encoding: 7bit
-
- In article <8BA8405.02C70020DE.uuout@sourcebbs.com>,
- DAVID MOHORN <david.mohorn@sourcebbs.com> wrote:
- >The only book that I know of that uses the notorious void main(void) is
- >his "Teach Yourself C" book. But he only uses this for the first half
- >of the book. This is only because he doesn't want the reader to be
- >overwhelmed with all the data types and other things until he has a
- >chance to explain how functions return values and pass arguments.
-
- That is no excuse, which is harder? To use 'int main()' consistantly
- thoughout a tutorial, and just say "trust me, I'll explain it all later", or
- to use 'void main()', and say "trust me, this works, so this is what I will
- use for now", and turn around and say "That is an incorrect way of defining
- 'main()' use this now." Which is more confusing to those who actually don't
- know what they are doing yet? I must be missing something, because I don't
- see why 'void main()' is any easier to "understand" than 'int main()',
- especially since you have to un-teach the 'void main()' later. I wonder
- what ever happened to "get it right the first time" attitude.
-
- >I haven't read the "Annotated Standard" book either.
-
- Here is the entire annotation for section 5.1.2.2.1 Program startup: Typos,
- and subscripts are mine.
-
- 5.1.2.2.1 Program startup: Remember, the names 'argc' and 'argv' are
- arbitrary. They could be any other legal identifiers you like. (However,
- using 'argc' and 'argv' follows generally accepted practice.)
-
- In most implementations, the return value from 'main()', if there is one
- [1], is returned to the operating system. Remember, if you don't explicitly
- return a value from 'main()' then the value passed to the operating system
- is, technically [2], undefined. Though most compilers [3] will automatically
- return 0 when no other return value is specified [4] (even when 'main()' is
- declared as 'void' [5]), you should not rely on this fact [6] because it is
- not guaranteed by the standard [7].
-
- The annotated ANSI C standard, pp 6-7
- Herbert Schildt
- (ISBN 0-07-881952-0)
-
- [1] There is ALWAYS a return value from main(), or at least the C startup
- code that calls main() thinks so. Where the value is grabbed from is
- anyone's guess, but if you put 'return 0;' at the end of 'main()', you
- will know where the return value comes from.
-
- [2] No technically about it, it IS undefined. Though an undefined return
- value is not as bad as undefined behavior (declairing 'main()' as void
- later in the paragraph). Hint on how to avoid this problem: always put
- 'return 0;' at the end of 'main()', unless you have a reason to put
- something else there.
-
- [3] How would the compiler know to return 0 at the end of 'main()' without
- the programmer telling it so? Hint: just put 'return 0;' at the end of
- main, and be sure what is being passed back.
-
- [4] How can you expect a randomly selected compiler to both (1) figure out
- that you did not return a value in 'main()' and (2) actually return
- something meaningful (0 in this case). Solution: put 'return 0;' at
- the end of 'main()', and not worry about it.
-
- [5] 'main()' may NOT be declared as 'void'. The more likely reason that
- the compiler appears to return 0, if no other value is returned is poor
- Mr. Schildt got very unlucky with a couple of test programs (if he in
- fact tested his theories, which is a dangerous practice to rely on), and
- passed on the results as fact.
-
- [6] What fact are we talking about there? compilers automatically return 0,
- even if you declare 'main()' as returning 'void', or forget to return a
- value from 'main()'. This is most definately not a fact, and a very
- inappropriate statement, especially when the standard on the opposite
- page doesn't support it.
-
- [7] damned straight! This is an annotation of the standard, so writing a
- back-handed blessing of wrong practices is inappropriate. This
- paragraph is a good example of what not to do, but unfortunately, Mr.
- Schildt failed to point that out. Unless that is what he meant by the
- last sentance. In which case, he gave too much information about what to
- expect for undefined behavior (implying that it is really not undefined
- in most cases).
-
- There are many more examples of wrong annotations, but reading them makes
- my head hurt. There is rumored to be a overly complete list of errors in
- the annotations available on the net. (I don't know where it is, neither do
- I care).
-